function rslice_hslice_redraw( hObject )
% RSLICE_HSLICE_REDRAW:  called everytime we need to refresh the horizontal view window.



if nargin == 0
	hObject = gcbo;
end
gdata = guidata ( hObject );


if gdata.verbose > 0
	rslice_message ( sprintf ( '%s:  variable is %s', mfilename, gdata.varname ) );
end

handles = guihandles ( hObject );



%
% Are there any user-defined graphics on screen?  We need to save those.
udh = findobj ( handles.horizontal_slice_axis, 'Tag', 'user defined rslice graphics' );
if ~isempty(udh)
	hgsave ( udh, 'user_defined_rslice_graphics.fig' );
end



old_position = get ( handles.horizontal_slice_axis, 'Position' );




%
% What is the proper time index?
file_time_index = gdata.timestep - gdata.ncfile_start_index(gdata.ncfile_index);
file_time = rslice_io ( hObject, gdata, 'file_time', file_time_index );


%
% Set the title string.
title_string = rslice_io ( hObject, gdata, 'title_string' );
variable_string = rslice_io ( hObject, gdata, 'variable_string' );
date_string = rslice_io ( hObject, gdata, 'date_string', file_time );
title_string_matrix = { title_string; variable_string; date_string};
set ( handles.horizontal_slice_title2, 'String', title_string_matrix );



%title_string_matrix = rslice_io ( hObject, gdata, 'title_string_matrix', title_string, variable_string );
gdata.grid = rslice_xyzm ( gdata );


vardata = rslice_io ( hObject, gdata, 'vardata', file_time_index );
vardata = double(vardata);






%
% account for the mask.
ind = find(gdata.grid.mask==0);
vardata(ind) = NaN;






%
% Clear out the colorbar
if isfield ( gdata, 'horizontal_slice_colorbar' )
    try
    	delete ( gdata.horizontal_slice_colorbar );
    catch
        %
        % Sometimes the handle is already invalid???
    end
end


axes ( handles.horizontal_slice_axis );
set ( handles.horizontal_slice_axis, 'NextPlot', 'Add' );
cla(handles.horizontal_slice_axis);
tag = get ( handles.horizontal_slice_axis, 'Tag' );
axes ( handles.horizontal_slice_axis );
h = pcolorjw ( gdata.grid.x, gdata.grid.y, vardata );



switch gdata.rslice_grid.coordsys
case 'geographic'
%  simple linear y-scaled projection
      xfac=cos(mean(gdata.grid.y(:))*pi/180);
      daspect([ 1 xfac 1] );
case 'xy'
      daspect([ 1 1 1]);
end

%set ( gdata.horizontal_slice_axis, 'Position', old_position' );





%
% Determine what the color limits will be.
if ( gdata.first_redraw == 0 )

	if ( gdata.autoscale_clims )

		%
		% Use the extrema of the current rendering.
		cmin = min ( vardata(:) );
		cmax = max ( vardata(:) );

	else

		%
		% Ok so we are not autoscaling, and this is not the
		% first rendering.  Use the limits determined from
		% the previous rendering.
		cmin = get ( handles.horizontal_cmin_edit, 'String' );
		cmin = str2double ( cmin );
		cmax = get ( handles.horizontal_cmax_edit, 'String' );
		cmax = str2double ( cmax );

	end


else

	%
	% If this is the first time thru, just use the min and
	% max of the current rendering.
	cmin = min ( vardata(:) );
	cmax = max ( vardata(:) );
end




%
% Setting the CLim property will croak if the limits are the same.
if (isnan(cmin) & isnan(cmax))
	cmin = 0; 
	cmax = 1;
end
if cmin == cmax
	if cmin == 0
		cmax = 1;
	else
		cmax = cmin + 1;
	end
end
set ( handles.horizontal_slice_axis, 'CLim', [cmin cmax] );
shading ( handles.horizontal_slice_axis, 'flat' );
%colorbar (handles.horizontal_slice_axis );
horizontal_slice_colorbar = colorbar ( 'peer', handles.horizontal_slice_axis );
%set ( horizontal_slice_colorbar, 'Position', [0.70    0.2087    0.0242    0.6315] );
set ( horizontal_slice_colorbar, 'Tag', 'Horizontal Slice Colorbar' );
gdata.horizontal_slice_colorbar = horizontal_slice_colorbar;






%
% If this is the first time thru, then set the clim text edits.
% We also need to do this if we are autoscaling.
if ( gdata.first_redraw == 1 ) | ( gdata.autoscale_clims )
	clim = get ( gca, 'CLim' );
	set ( handles.horizontal_cmin_edit, 'String', num2str ( clim(1) ) );
	set ( handles.horizontal_cmax_edit, 'String', num2str ( clim(2) ) );
	gdata.first_redraw = 0;
	
	%
	% Also set the timestep edit string
	theString = sprintf ( 'Timestep (%d)', gdata.max_num_timesteps );
	set ( handles.timestep_text, 'String', theString );
end

%
% Also set the depth edit string
if gdata.hslice_on_sigma
	switch ( gdata.dim_type )
	case 'TZYX'
		theString = sprintf ( 'Sigma (%d)', gdata.varinfo.Size(2) );
	case { 'TYX', 'YX' }
		theString = sprintf ( '' );
	end
else
	switch ( gdata.dim_type )
	case 'TZYX'
		theString = sprintf ( 'Depth' );
	case { 'TYX', 'YX' }
		theString = sprintf ( '' );
	end
end

set ( handles.depth_text, 'String', theString );


%
% What about velocity arrows?
hvo_handles = guihandles ( gdata.hvoc_figure );
hvo_data = guidata ( gdata.hvoc_figure );
draw_velocity_arrows = get ( hvo_handles.show_hide_arrows_toggle, 'Value' );
if ( draw_velocity_arrows == 1 )
	rslice_velocity_overlay_redraw ( hObject );
end



%
% Load any user defined graphics.
try
	udh = hgload ( 'user_defined_rslice_graphics.fig' );
end

guidata ( hObject, gdata );
